package com.example.allplan.ShuJuKuCaoZuo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

import com.example.allplan.JavaBean.BiaoQingLieBiao;
import com.example.allplan.JavaBean.JiHuaBaoJavaBean;
import com.example.allplan.JavaBean.JiHuaTiaoJavaBean;
import com.example.allplan.JavaBean.JiHuaXinXiJavaBean;
import com.example.allplan.RecyclerAdapter.JiHuaBaoRecyclerAdapter;
import com.example.allplan.SQhelper.SuoYouJiHuaBaoSQhelper;
import com.example.allplan.SQhelper.YiGeJiHuaSQhelper;

import java.util.ArrayList;

public class JiHuaSQCaoZuo {
    public  String wen_jian_ming;
    public YiGeJiHuaSQhelper mdbhelp;
    public SQLiteDatabase mDatabase;
    public Context mContext;
    public static ContentValues bean_to_values(JiHuaTiaoJavaBean o){
        ContentValues values=new ContentValues();
        values.put("id",o.getId());
        values.put("pai_xu_you_biao",o.getPai_xu_you_biao());
        values.put("chu_shi_zhi",o.getChu_shi_zhi());
        values.put("xian_zai_zhi",o.getXian_zai_zhi());
        values.put("ming_cheng",o.getMingcheng());
        return values;
    }
    public static ContentValues xinxibean_to_value(JiHuaXinXiJavaBean o){
        ContentValues values=new ContentValues();
        values.put("lei_xing",o.getLei_xing());
        values.put("tu_pian_ge_shu",o.getTu_pian_ge_shu());
        values.put("ji_hua_id",o.getJi_hua_id());
        values.put("wen_jian_ming",o.getWen_jian_ming());
        values.put("biao_ti",o.getBiao_ti());
        values.put("id_you_biao",o.getId_you_biao());
        return values;
    }
   /* public static ContentValues bean_to_value_only_biao_qing_and_bao_ming(JiHuaBaoJavaBean o){
        ContentValues values=new ContentValues();
        values.put("id",o.getId());
        values.put("biao_qing_fu_hao_zhi",o.getBiao_qing_fu_hao_zhi());
        values.put("bao_ming",o.getBao_ming());
        return values;
    }*/
   public static ContentValues xinxibean_to_value_only_three(JiHuaXinXiJavaBean o){
       ContentValues values=new ContentValues();
       values.put("lei_xing",o.getLei_xing());
       values.put("biao_ti",o.getBiao_ti());
       values.put("id_you_biao",o.getId_you_biao());
       return values;
   }
    public JiHuaSQCaoZuo(Context mC,String ms) {
        wen_jian_ming=ms;
        mContext=mC;
        mdbhelp=new YiGeJiHuaSQhelper(mContext,ms,null,1);
    }
    public synchronized void openRead(){
        mDatabase=mdbhelp.getReadableDatabase();
    }
    public synchronized void close(){
        mDatabase.close();
    }
    public synchronized void xie_ru_xin_xi(JiHuaXinXiJavaBean o){
        openWrite();
        mDatabase.insert("xin_xi",null,xinxibean_to_value(o));
        close();
    }
    public synchronized void geng_xin_xin_xi_only_three(JiHuaXinXiJavaBean o){
        openWrite();
        mDatabase.update("xin_xi",xinxibean_to_value_only_three(o),"ji_hua_id=?",new String[]{""+o.getJi_hua_id()});
        close();
    }
    public synchronized JiHuaXinXiJavaBean du_qu_xinxi(){
        openWrite();
        Cursor cursor = mDatabase.query("xin_xi", new String[]{"id_you_biao","lei_xing","tu_pian_ge_shu","ji_hua_id","wen_jian_ming" ,"biao_ti" }, null, null, null, null, "id_you_biao");
        cursor.moveToNext();
        return new JiHuaXinXiJavaBean(cursor.getInt(cursor.getColumnIndex("id_you_biao")),cursor.getInt(cursor.getColumnIndex("lei_xing")),cursor.getInt(cursor.getColumnIndex("tu_pian_ge_shu")),cursor.getInt(cursor.getColumnIndex("ji_hua_id")),cursor.getString(cursor.getColumnIndex("wen_jian_ming")),cursor.getString(cursor.getColumnIndex("biao_ti")));
    }
    public synchronized void openWrite(){
        mDatabase=mdbhelp.getWritableDatabase();
    }
    public synchronized ArrayList<JiHuaTiaoJavaBean> reloadAllJiHuaTiao(){
        ArrayList<JiHuaTiaoJavaBean> mList=new ArrayList<JiHuaTiaoJavaBean>();
        openWrite();
        Cursor cursor = mDatabase.query("ji_hua_tiao_biao", new String[]{"id","chu_shi_zhi","xian_zai_zhi","pai_xu_you_biao","ming_cheng"}, null, null, null, null, "id");
        while (cursor.moveToNext()) {
            mList.add(new JiHuaTiaoJavaBean(cursor.getInt(cursor.getColumnIndex("id")),cursor.getInt(cursor.getColumnIndex("chu_shi_zhi")),cursor.getInt(cursor.getColumnIndex("xian_zai_zhi")),cursor.getInt(cursor.getColumnIndex("pai_xu_you_biao")),cursor.getString(cursor.getColumnIndex("ming_cheng")),cursor.getInt(cursor.getColumnIndex("xian_zai_zhi"))));
        }
        close();
        return mList;
    }
   /* public synchronized JiHuaBaoJavaBean reloadByIdJiHua(int n){
        JiHuaBaoJavaBean mo=new JiHuaBaoJavaBean();
        openWrite();
        Cursor cursor = mDatabase.query("ji_hua_bao_biao", new String[]{"id","pai_xu_you_biao","biao_qing_fu_hao_zhi","lei_xing","ji_hua_shu","bao_ming"}, "id=?", new String[]{n+""}, null, null, "id");
        cursor.moveToNext();
        mo =new JiHuaBaoJavaBean(cursor.getInt(cursor.getColumnIndex("id")),cursor.getInt(cursor.getColumnIndex("pai_xu_you_biao")),cursor.getInt(cursor.getColumnIndex("biao_qing_fu_hao_zhi")),cursor.getInt(cursor.getColumnIndex("lei_xing")),cursor.getInt(cursor.getColumnIndex("ji_hua_shu")),cursor.getString(cursor.getColumnIndex("bao_ming")));

        close();
        return mo;
    }*/
   public  synchronized void write_shu_zu(ArrayList<JiHuaTiaoJavaBean> ml){
       openWrite();
       for(int i=0;i<ml.size();i++){
           mDatabase.insert("ji_hua_tiao_biao",null,bean_to_values(ml.get(i)));
       }
       close();
   }
    public synchronized void write_one(JiHuaTiaoJavaBean o){
        openWrite();
        mDatabase.insert("ji_hua_tiao_biao",null,bean_to_values(o));
        close();
    }
    public synchronized void update_one(JiHuaTiaoJavaBean o){
        openWrite();
        mDatabase.update("ji_hua_tiao_biao",bean_to_values(o),"id=?",new String[]{o.getId()+""});
        close();
    }
    public synchronized void update_suo_you(ArrayList<JiHuaTiaoJavaBean> l){
        openWrite();
        for(int i=0;i<l.size();i++){
            mDatabase.update("ji_hua_tiao_biao",bean_to_values(l.get(i)),"id=?",new String[]{l.get(i).getId()+""});

        }
        close();
    }
    /*public synchronized void update_one_only_biao_qing_and_bao_ming(JiHuaBaoJavaBean o){
        openWrite();
        mDatabase.update("ji_hua_bao_biao",bean_to_value_only_biao_qing_and_bao_ming(o),"id=?",new String[]{o.getId()+""});
        close();
    }*/
    public  synchronized void delete_suo_you_chuang_jian(){
        openWrite();
        mDatabase.delete("ji_hua_tiao_biao",null,null);
        close();
    }
}
